﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BCGC.BLL.BusinessObject;
using Jalal.Library.Utility;
using BCGC.BLL.Helper;

public partial class Admin_Settings : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
    }

    private void LoadData()
    {
        Settings settings = Settings.SiteSettings;
        if (settings != null)
        {
            tbxAdminEmail.Text = settings.AdminEmail;
            tbxAdminUsername.Text = settings.AdminUsername;
            tbxCompanyEmail.Text = settings.CompanyEmail;
            tbxCompanySiteUrl.Text = settings.CompanySiteUrl;
            tbxEncryptionKey.Text = settings.EncryptionKey;
            tbxMailPort.Text = settings.MailPort.ToString();
            tbxMailServer.Text = settings.MailServer;
            tbxMailUsername.Text = settings.MailUsername;
            tbxSiteName.Text = settings.SiteName;
            tbxSiteUrl.Text = settings.SiteUrl;
            tbxSupportEmail.Text = settings.SupportEmail;
            chkUseSSLMail.Checked = settings.UseSSLMail;
        }
    }

    protected void Update_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            Settings settings = Settings.SiteSettings;
            settings.AdminEmail = Security.BindStringInput(tbxAdminEmail.Text, SecurityLevel.Three);
            settings.AdminUsername = Security.BindStringInput(tbxAdminUsername.Text, SecurityLevel.Three);
            settings.CompanyEmail = Security.BindStringInput(tbxCompanyEmail.Text, SecurityLevel.Three);
            settings.CompanySiteUrl = Security.BindStringInput(tbxCompanySiteUrl.Text, SecurityLevel.Three);
            settings.EncryptionKey = Security.BindStringInput(tbxEncryptionKey.Text, SecurityLevel.Three);
            settings.MailPort = Security.BindIntegerInput(tbxMailPort.Text, SecurityLevel.Three);
            settings.MailServer = Security.BindStringInput(tbxMailServer.Text, SecurityLevel.Three);
            settings.MailUsername = Security.BindStringInput(tbxMailUsername.Text, SecurityLevel.Three);
            settings.SiteName = Security.BindStringInput(tbxSiteName.Text, SecurityLevel.Three);
            settings.SiteUrl = Security.BindStringInput(tbxSiteUrl.Text, SecurityLevel.Three);

            if (!string.IsNullOrEmpty(tbxNewPassword.Text) && tbxNewPassword.Text == tbxConfirmPassword.Text)
            {
                settings.AdminPassword = Security.BindStringInput(tbxNewPassword.Text, SecurityLevel.Three);
            }

            if (!string.IsNullOrEmpty(tbxNewMailPassword.Text) && 
                tbxNewMailPassword.Text != settings.MailPassword
                && tbxNewMailPassword.Text == tbxConfirmMailPassword.Text && 
                tbxOldMailPassword.Text == settings.MailPassword)
            {
                settings.MailPassword = Security.BindStringInput(tbxNewMailPassword.Text, SecurityLevel.Three);
            }

            if (settings.Update())
                uccResponse.SetSuccessMessage("Settings updated successfuly");
            else
                uccResponse.SetFailMessage("Failed to update settings");
        }
    }
}
